package com.hbue.springboot.controller;

import ch.qos.logback.classic.Logger;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hbue.springboot.pojo.DTO.TbHistoryDTO;
import com.hbue.springboot.pojo.Result;
import com.hbue.springboot.pojo.TbCustomer;
import com.hbue.springboot.pojo.TbHistory;
import com.hbue.springboot.service.TbAccountService;
import com.hbue.springboot.service.TbCustomerService;
import com.hbue.springboot.utils.CommonPage;
import com.hbue.springboot.utils.ResultCode;
import com.hbue.springboot.vo.AddOrderVo;
import com.hbue.springboot.vo.QueryParamsVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/customer")
@RequiredArgsConstructor
public class TbCustomerController {
    private final TbCustomerService tbCustomerService;

    @PostMapping("/update")
    public Result update(@RequestBody TbCustomer tbCustomer) {
//        Logger log;
        log.info("update customer = "+tbCustomer);
        int rows = tbCustomerService.updateCustomer(tbCustomer);
        log.info("update customer row = "+rows);
        if (rows > 0) {
            return Result.success(rows);
        } else {
            return Result.error(ResultCode.FAIL);
        }
    }

    /**
     *  显示未完成订单
     */
    @PostMapping("/getOrderWithoutFinish")
    public Result getOrderWithoutFinish(@RequestBody QueryParamsVo parms) {
        IPage<TbHistory> list = tbCustomerService.getOrderWithoutFinish(parms);
        if(list != null) {
            return Result.success(list);
        } else {
            return Result.error(ResultCode.NOTFOUND);
        }
    }

    /**
     * 顾客下单
     * @param order
     * @return
     */
    @PostMapping("/addOrder")
    public Result addOrder(@RequestBody AddOrderVo order) {
        log.info("add order =" + order);

        try {

            int row = tbCustomerService.addOrder(order);
            if(row > 0) {
                return Result.success();
            } else if(row == -3) {
                return Result.error(ResultCode.MONEY_ERROR);
            } else if(row == -2) {
                return Result.error(ResultCode.USER_NOT_EXIST);
            } else {
                return Result.error(String.valueOf(row));
            }

        } catch (Exception e) {
            return Result.error(ResultCode.INSERT_FAIL);
        }
    }

    /**
     * 顾客取消订单
     * @param id
     * @return
     */
    @PostMapping("/cancelOrder")
    public Result cancelOrder(@RequestParam Integer id) {
        log.info("cancel order =" + id);
        try {

            int row = tbCustomerService.cancelOrder(id);

            if(row > 0) {
                return Result.success(row);
            } else if (row == -3) {
                return Result.error(ResultCode.ORDER_FINISH);
            }else
                return Result.error(ResultCode.FAIL);

        } catch(Exception e) {
            return Result.error(ResultCode.DELETE_FAIL);
        }
    }
    @GetMapping("/getName")
    public Result getName(String name) {
        TbCustomer customer = tbCustomerService.getCustomerByName(name);
        if(customer == null) {
            return Result.error(ResultCode.FAIL);
        }
        return Result.success(customer);
    }

    @PostMapping("/getNameById")
    public Result getCustomerName(@RequestParam Integer id){
        return Result.success(tbCustomerService.getCustomerName(id));
    }

    @PostMapping("/getAddressById")
    public Result getCustomerAddress(@RequestParam Integer id){
        return Result.success(tbCustomerService.getCustomerAddress(id));
    }
}